package kitwee.com.kanpad;

import android.content.Context;

import java.util.List;

import greendao.gen.DaoSession;
import greendao.gen.SocketPortDataDao;
import kitwee.com.kanpad.bean.SocketPortData;

/**
 * Created by ganquan on 2019/4/2 23:06
 */
public class SocketDataDaoUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private SocketPortDataDao studentDao;
    private DaoSession daoSession;

    public SocketDataDaoUtil(Context context) {
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertSocket(SocketPortData student) {
        manager.getDaoSession().insertOrReplace(student);
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultSocket(final List<SocketPortData> datas) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (SocketPortData socketPortData : datas) {
                    manager.getDaoSession().insertOrReplace(socketPortData);
                }
            }
        });
    }


    /**
     * 删除数据
     */
    public void deleteSocket(SocketPortData data) {
        manager.getDaoSession().delete(data);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll() {
        manager.getDaoSession().getSocketPortDataDao().deleteAll();
    }

    /**
     * 更新数据
     */
    public void updateSocket(SocketPortData student) {
        manager.getDaoSession().update(student);
    }

    /**
     * 按照主键返回单条数据
     */
    public SocketPortData listOneStudent(long key) {
        return manager.getDaoSession().load(SocketPortData.class, key);
    }


    /**
     * 查询全部数据
     */
    public List<SocketPortData> queryAll() {
        return manager.getDaoSession().loadAll(SocketPortData.class);
    }
}
